package com.lngsyz.ywtb.biz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lngsyz.ywtb.biz.service.HxService;
import com.lngsyz.ywtb.entity.CommonConstant;
import com.lngsyz.ywtb.entity.LoginInfo;
import com.lngsyz.ywtb.entity.URIResult;
import com.lngsyz.ywtb.entity.hx.AddHxElecOrder;
import com.lngsyz.ywtb.entity.hx.AddHxPhyOrder;
import com.lngsyz.ywtb.entity.hx.GetHxList;
import com.lngsyz.ywtb.entity.hx.OutHxList;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@Api(tags = "HxController", description = "航信专用")
@RequestMapping("v1/hx")
public class HxController {

	@Autowired()
	private HxService hxService;

	@ApiOperation(value = "获取token", notes = "通过授权信息获取token")
	@RequestMapping(value = "/getToken", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> getToken(
			@ApiParam(name = "appId", value = "应用id") @RequestParam(value = "appId", defaultValue = "") String appId,
			@ApiParam(name = "appSecret", value = "授权密钥") @RequestParam(value = "appSecret", defaultValue = "") String appSecret,
			@ApiParam(name = "userId", value = "用户唯一值，可为空") @RequestParam(value = "userId", defaultValue = "") String userId,
			HttpServletRequest request) {
		URIResult<String> res = new URIResult<String>();
		try {
			String tokenStr = hxService.getToken(appId, appSecret, userId);
			res.setData(tokenStr);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "新增物理订单", notes = "新增物理订单")
	@ApiImplicitParam(name = "addHxPhyOrder", value = "新增物理订单", dataType = "AddHxPhyOrder", required = true, paramType = "body")
	@RequestMapping(value = "/addPhyOrder", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> addPhyOrder(@RequestBody AddHxPhyOrder addHxPhyOrder,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			hxService.addPhyOrder(addHxPhyOrder, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "新增电子订单", notes = "新增电子订单")
	@ApiImplicitParam(name = "addHxElecOrder", value = "新增电子订单", dataType = "AddHxElecOrder", required = true, paramType = "body")
	@RequestMapping(value = "/addElecOrder", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> addElecOrder(@RequestBody AddHxElecOrder addHxElecOrder,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			hxService.addElecOrder(addHxElecOrder, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取申请列表", notes = "获取申请列表")
	@RequestMapping(value = "/getList", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<OutHxList>> getList(
			@ApiParam(name = "accept", value = "承接状态（0：未承接 1：已承接）") @RequestParam(value = "accept", defaultValue = "") String accept,
			@ApiParam(name = "companyId", value = "刻章厂ID") @RequestParam(value = "companyId", defaultValue = "") String companyId,
			@ApiParam(name = "customName", value = "用章单位名") @RequestParam(value = "customName", defaultValue = "") String customName,
			@ApiParam(name = "customInstCode", value = "用章单位信用代码") @RequestParam(value = "customInstCode", defaultValue = "") String customInstCode,
			@ApiParam(name = "page", value = "分页页码", required = true) @RequestParam(value = "page", defaultValue = "1") int page,
			@ApiParam(name = "size", value = "每页显示数量", required = true) @RequestParam(value = "size", defaultValue = "10") int size,
			HttpServletRequest request) {
		URIResult<List<OutHxList>> res = new URIResult<List<OutHxList>>();
		try {
			GetHxList getHxList = new GetHxList();
			getHxList.setAccept(accept);
			getHxList.setCompanyId(companyId);
			getHxList.setCustomName(customName);
			getHxList.setCustomInstCode(customInstCode);
			List<OutHxList> infos = hxService.getList(getHxList, page, size);
			int total = hxService.getCount(getHxList);
			res.setData(infos);
			res.setTotal(total);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	

	@ApiOperation(value = "承接", notes = "承接")
	@ApiImplicitParam(name = "addHxElecOrder", value = "承接", dataType = "AddHxElecOrder", required = true, paramType = "body")
	@RequestMapping(value = "/accept", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> accept(@RequestBody AddHxElecOrder addHxElecOrder,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			hxService.addElecOrder(addHxElecOrder, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
}